package com.zhuchaooasystem.presenter.home.fragment.oa.daily;

import android.app.Activity;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.zhuchaooasystem.api.HttpApi;
import com.zhuchaooasystem.api.HttpFile;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.entity.NewDailyEntity;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.utils.ToastUtil;
import com.zhuchaooasystem.widget.CameraPickDialog;

import java.util.List;
import java.util.Map;

/**
 * @author yinsi
 */


public class NewDailyPresenter extends BasePresenter<NewDailyContract.View> implements NewDailyContract.Presenter {

    public NewDailyPresenter(Activity activity, NewDailyContract.View view) {
        super(activity, view);
    }

    @Override
    public void showPickDialog() {
        CameraPickDialog dialog = new CameraPickDialog(mActivity);
        dialog.show();
        dialog.setOnViewClickListener(new CameraPickDialog.onViewClickListenner() {
            @Override
            public void onCamera(CameraPickDialog dialog) {
                dialog.dismiss();
                mView.onCameraClick();
            }

            @Override
            public void onAlbum(CameraPickDialog dialog) {
                dialog.dismiss();
                mView.onAlbumClick();
            }

        });
    }

    @Override
    public void submit() {
        String content = mView.getContent();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.showShort(mActivity, "请输入工作内容");
            return;
        }
        mView.setClickable(false);
        showLoading();
        //----
        final int userId = UserInfo.getInstance().getMemberId();
        String username = UserInfo.getInstance().getUserName();

        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("PersonnelPK", userId + "");
        map.put("contentMessage", content);
        map.put("userName", username);

        List<String> imageList = mView.getImageList();
        //预加了一张添加图片，list_images.size() 大于 1表示有图片，同时上传的时候要去掉预加的图片
        if (null != imageList && imageList.size() > 0) {
            imageList.remove(imageList.size() - 1);
        }
        HttpFile.upload(mActivity, HttpApi.submitWorkDay, map, imageList, true,new HttpFile.HttpCallBack() {

            @Override
            public void onSusscess(String data) {
                NewDailyEntity entity = new Gson().fromJson(data, NewDailyEntity.class);
                if (null != entity && null != entity.getResult().getSuccessful()) {
                    mView.submitSuccuss();
                } else {
                    ToastUtil.showShort(mActivity, entity.getResult().getError());
                }
                LoadingDiaogDismiss();
            }

            @Override
            public void onError(String meg) {
                super.onError(meg);
                LoadingDiaogDismiss();
            }
        });


    }
}
